package com.svmc.app.task.view;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.svmc.app.task.activity.R;
import com.svmc.app.task.activity.SettingActivity;
import com.svmc.app.task.adapter.DbAdapter;
import com.svmc.app.task.alarm.DialogAlarm;
import com.svmc.app.task.model.TaskModel;
import com.svmc.app.task.utils.CommonDefine;

public class CustomDialog extends Dialog implements
		android.view.View.OnClickListener, CommonDefine {
	// private final static int DIALOG_INFO = 1;

	private int dialogType;
	private Context context;
	TextView lb_task, note, timefinish;
	ImageView btn_okie, btn_cancel;
	TextView txt_message;
	TaskModel taskmodel;
	ListView list_choose_setting;
	TextView title_dialog_choose;
	public CustomDialog(Context context) {
		super(context);
		this.context = context;
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.getWindow().setBackgroundDrawable(
				new ColorDrawable(Color.argb(0, 255, 255, 255)));

	}
	
	public void setDialogType(int dialogType,String message){
		this.dialogType = dialogType;
		switch (dialogType) {
		case DIALOG_NOTIFY:
		case EMAIl_SEND_SUCCESS:
		case EMAIl_SEND_FAIL:
			this.setContentView(R.layout.dialog_email_result);
			lb_task =(TextView)findViewById(R.id.dialog_email_result_txt_lbtask);
			note =(TextView)findViewById(R.id.dialog_email_result_txt_label_note);
			lb_task.setText(context.getString(R.string.feedback_email_result));
			note.setText(message);
			btn_okie =(ImageView)findViewById(R.id.dialog_email_result_img_okie);
			btn_okie.setOnClickListener(this);
			break;		
		default :
			break;
		}
	}

	public void setDialogType(int dialogType, String title, String notetask,
			String time) {
		this.dialogType = dialogType;
		switch (dialogType) {
		case DIALOG_INFO:
			this.setContentView(R.layout.dialog_info);

			lb_task = (TextView) findViewById(R.id.dialoginfo_txt_lbtask);
			note = (TextView) findViewById(R.id.dialoginfo_txt_note);
			timefinish = (TextView) findViewById(R.id.dialoginfo_txt_timefinish);
			btn_okie = (ImageView) findViewById(R.id.dialoginfo_img_okie);
			btn_okie.setOnClickListener(this);

			lb_task.setText(title);
			note.setText(notetask);
			timefinish.setText(time);
			break;
		case DIALOG_ALARM:
			this.setContentView(R.layout.dialog_info);

			lb_task = (TextView) findViewById(R.id.dialoginfo_txt_lbtask);
			note = (TextView) findViewById(R.id.dialoginfo_txt_note);
			timefinish = (TextView) findViewById(R.id.dialoginfo_txt_timefinish);
			btn_okie = (ImageView) findViewById(R.id.dialoginfo_img_okie);
			btn_okie.setOnClickListener(this);
			lb_task.setText(title);
			note.setText(notetask);
			timefinish.setText(time);

			break;
		default:
			break;
		}

	}

	public void setDialogType(int dialogType, String message, TaskModel task) {
		this.dialogType = dialogType;
		switch (dialogType) {
		case DIALOG_CONFIRM_DELETE:
			this.setContentView(R.layout.dialog_confirm);
			lb_task = (TextView) findViewById(R.id.dialogconfirm_txt_lbtask);
			lb_task.setText(getContext().getString(
					R.string.dialog_confirm_label));
			txt_message = (TextView) findViewById(R.id.dialogconfirm_txt_message);
			txt_message.setText(message);
			btn_okie = (ImageView) findViewById(R.id.dialogconfirm_img_okie);
			btn_cancel = (ImageView) findViewById(R.id.dialogconfirm_img_cancel);
			btn_okie.setOnClickListener(this);
			btn_cancel.setOnClickListener(this);
			taskmodel = task;
			break;
		default:
			break;
		}
	}

	public void setDialogType(int dialogType,String title, ArrayList<String> data) {
		this.dialogType = dialogType;
		switch (dialogType) {
		case DIALOG_CHOOSE_SETTING :
			this.setContentView(R.layout.dialog_choose_setting);
			title_dialog_choose =(TextView)findViewById(R.id.dialog_setting_choose_title);
			title_dialog_choose.setText(title);
			list_choose_setting =(ListView)findViewById(R.id.dialog_choose_setting_listview);
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, data);
			list_choose_setting.setAdapter(adapter);
			listenOnlickListview(data,title);
			break;
		default :
			break;
		}
	}

	public void onClick(View v) {
		int id = v.getId();
		switch (dialogType) {
		
		case DIALOG_INFO:
			if (id == R.id.dialoginfo_img_okie) {
				this.dismiss();
			}
			break;
		case DIALOG_ALARM:
			if (id == R.id.dialoginfo_img_okie) {
				DialogAlarm.stopAudioAlarm();
				this.dismiss();
				((Activity) context).finish();
			}
			break;
		case DIALOG_CONFIRM_DELETE:
			if (id == R.id.dialogconfirm_img_okie) {
				DbAdapter db = new DbAdapter(context);
				int indexrow = db.deleteTask(taskmodel);
				if (indexrow > 0) {
					showToast("Delete susscessfull");

					this.dismiss();
				} else {
					showToast("Delele not susscess");
				}
			}
			if (id == R.id.dialogconfirm_img_cancel) {
				this.dismiss();
			}
			break;
		case DIALOG_NOTIFY:
		case EMAIl_SEND_FAIL:
			if(id==R.id.dialog_email_result_img_okie){
				this.dismiss();
			}
			break;
		default:
			break;
		}

	}

	public void showToast(String message) {
		Toast.makeText(context, message, Toast.LENGTH_LONG).show();
	}
	
	public void listenOnlickListview(final ArrayList<String> data,final String title){
		list_choose_setting.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				
				savePreferences(title, data.get(position));
				//showToast(getPreferences(DELETE_SCHEDULE_TYPE_SETTING));
				SettingActivity.dateformat.setText(getPreferences(DATE_FORMAT_TYPE));
				SettingActivity.timeformat.setText(getPreferences(TIME_FORMAT_TYPE));
				SettingActivity.deleteschedule.setText(getPreferences(DELETE_SCHEDULE_TYPE_SETTING));
				SettingActivity.alarmFrequency.setText(getPreferences(ALARM_FREQUENCY_TYPE_SETTING));
				CustomDialog.this.dismiss();
				
			}
		});
	}
	public void savePreferences(String type, String data ){
		SharedPreferences dataSetting= PreferenceManager.getDefaultSharedPreferences(context);
		SharedPreferences.Editor editor = dataSetting.edit();
		int typeSetting=0;
		if(type.equalsIgnoreCase(context.getString(R.string.title_dialog_choose_setting_alarm_frequency))){
			typeSetting=ALARM_FREQUENCY_SETTING;
		}else if (type.equalsIgnoreCase(context.getString(R.string.title_dialog_choose_setting_delete_scheduele))) {
			typeSetting=DELETE_SCHEDULE_SETTING;
		}else if (type.equalsIgnoreCase(context.getString(R.string.title_dialog_choose_setting_date_format))) {
			typeSetting=DATE_FORMAT_SETTING;
		}else if (type.equalsIgnoreCase(context.getString(R.string.title_dialog_choose_setting_time_format))) {
			typeSetting=TIME_FORMAT_SETTING;
		}
		switch (typeSetting) {
		case DELETE_SCHEDULE_SETTING:
			editor.putString(DELETE_SCHEDULE_TYPE_SETTING, data);
			editor.commit();
			break;
		case ALARM_FREQUENCY_SETTING:
			editor.putString(ALARM_FREQUENCY_TYPE_SETTING, data);
			editor.commit();
			break;
		case DATE_FORMAT_SETTING:
			editor.putString(DATE_FORMAT_TYPE, data);
			editor.commit();
			break;
		case TIME_FORMAT_SETTING:
			editor.putString(TIME_FORMAT_TYPE, data);
			editor.commit();
			break;
		default:
			break;
		}
	}
	public String getPreferences(String type){
		String temp="";
		SharedPreferences dataSetting= PreferenceManager.getDefaultSharedPreferences(context);
		temp= dataSetting.getString(type, "OFF");
		return temp;
	}
	
	


}
